var maxSubArray = function (nums) {
  let pre = 0,
  maxAns = nums[0];
  nums.forEach((x) => {
    pre = Math.max(pre + x, x);
    maxAns = Math.max(maxAns, pre);
  });
  return maxAns;
};
nums = [-2,1,-3,4,-1,2,1,-5,4]
console.log(maxSubArray(nums));

/* 输入：nums = [-2,1,-3,4,-1,2,1,-5,4]
输出：6
解释：连续子数组 [4,-1,2,1] 的和最大，为 6 。 */
